home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / UNITS / ANIMAP.CPP < prev    next >
C/C++ Source or Header  |  1993-02-27  |  11KB  |  323 lines

  1. #include "stddefs.h"
  2. #include <stdlib.h>
  3. #include <fstream.h>
  4. #include <string.h>
  5. #include "icon.h"
  6. #include "animap.h"
  7. #include "xlib.h"
  8. #include "actor.h"
  9.  
  10. inline int rollover(int number, int limit)
  11. {
  12.   int temp = number % limit;
  13.   temp += (temp < 0) ? limit : 0;
  14.   return temp;
  15. //  return number & (limit-1);
  16. }
  17.  
  18. void animapSquare::display(int x, int y, word pageBase, int squareWidth)
  19. {
  20.   draw(x, y, pageBase);
  21.   if (nextActor)
  22.     drawActors(x + thisFrame->picture.width - squareWidth,
  23.            y + thisFrame->picture.height - squareWidth, pageBase);
  24. }
  25.  
  26. void animapSquare::drawActors(int x, int y, word pageBase)
  27. {
  28.   animactor * this_actor = nextActor;
  29.   while (this_actor != NULL)
  30.   {
  31.     this_actor->draw(x - this_actor->thisFrame->picture.width + this_actor->squareX,
  32.     y - this_actor->thisFrame->picture.height + this_actor->squareY, pageBase);
  33.     this_actor = this_actor->nextActor;
  34.   }
  35. }
  36.  
  37. void animapSquare::advance(void)
  38. #define MAXINASQUARE 20
  39. {
  40.   static animactor * voodooActors[MAXINASQUARE];
  41.   animslave::advance();
  42.   animactor ** voodooCounter = &voodooActors[0];
  43.   animactor * this_actor = nextActor;
  44.   while (this_actor != NULL)
  45.   {
  46.     *(voodooCounter++) = this_actor;
  47.     this_actor = this_actor->nextActor;
  48.   }
  49.   *voodooCounter = NULL;
  50.   voodooCounter = &voodooActors[0];
  51.   while (*voodooCounter != NULL)
  52.   {
  53.     (*voodooCounter)->advance();
  54.     ++voodooCounter;
  55.   }
  56. }
  57.  
  58. void * animap::isInSquare(int x, int y, byte searchIdentity)
  59. {
  60.   x %= (int)(width);
  61.   y %= (int)(height);
  62.   x += (x < 0) ? width : 0;
  63.   y += (y < 0) ? height : 0;
  64.   animactor * this_actor = mapData[x][y].nextActor;
  65.   while (this_actor != NULL)
  66.   {
  67.     if (this_actor->myIdentity == searchIdentity)
  68.       return(this_actor);
  69.     this_actor = this_actor->nextActor;
  70.   }
  71.   return NULL;
  72. }
  73.  
  74.  
  75. animap::animap(int inumIcons, int sqwidth, int iwidth, int iheight)
  76. {
  77.   typedef animapSquare * mapptr;
  78.   numIcons = inumIcons;
  79.   width = iwidth;
  80.   height = iheight;
  81.   squareWidth = sqwidth;
  82.   mapData = new mapptr[width];
  83.   for(int widthCounter = 0; widthCounter < width; ++widthCounter)
  84.     {
  85.     mapData[widthCounter] = new animapSquare[height];
  86.     }
  87.   iconTable = new animicon[numIcons];
  88.   iconNames = new fileNameString[numIcons];
  89.   for (int iconCounter = 0; iconCounter < numIcons; ++iconCounter)
  90.   {
  91.     iconTable[iconCounter].firstFrame =
  92.     iconTable[iconCounter].lastFrame =
  93.     iconTable[iconCounter].thisFrame = NULL;
  94.     strcpy(iconNames[iconCounter], "");
  95.   }
  96. }
  97.  
  98. animap::~animap()
  99. {
  100.   for (int widthCounter = 0; widthCounter < width; ++widthCounter)
  101.     delete mapData[widthCounter];
  102.   delete mapData;
  103.   for(widthCounter = 0; widthCounter < numIcons; ++widthCounter)
  104.   {
  105.     iconTable[widthCounter].animicon::~animicon();
  106.   }
  107.   delete iconTable;
  108.   delete iconNames;
  109. };
  110.  
  111. animap::animap(char * filename, yakLib * myYakLib)
  112. {
  113.   int inumIcons, sqwidth, iwidth, iheight;
  114.   ifstream infile(filename, ios::in | ios::binary);
  115.   if (!infile) return;
  116.   infile >> inumIcons >> sqwidth >> iwidth >> iheight;
  117.   typedef animapSquare * mapptr;
  118.   numIcons = inumIcons;
  119.   width = iwidth;
  120.   height = iheight;
  121.   squareWidth = sqwidth;
  122.   mapData = new mapptr[width];
  123.   for(int widthCounter = 0; widthCounter < width; ++widthCounter)
  124.     {
  125.     mapData[widthCounter] = new animapSquare[height];
  126.     }
  127.   iconTable = new animicon[numIcons];
  128.   iconNames = new fileNameString[numIcons];
  129.   for (int iconCounter = 0; iconCounter < numIcons; ++iconCounter)
  130.   {
  131.     iconTable[iconCounter].firstFrame =
  132.     iconTable[iconCounter].lastFrame =
  133.     iconTable[iconCounter].thisFrame = NULL;
  134.     strcpy(iconNames[iconCounter], "");
  135.   }
  136.   for (int counter = 0; counter < numIcons; ++counter)
  137.   {
  138.     infile >> iconNames[counter];
  139.     loadIcon(counter, iconNames[counter], myYakLib);
  140.   }
  141.   for (word heightCounter = 0; heightCounter < height; ++heightCounter)
  142.   {
  143.     for (word widthCounter = 0; widthCounter < width; ++widthCounter)
  144.     {
  145.       infile >> mapData[widthCounter][heightCounter].myIconNumber;
  146.       setSquare(widthCounter, heightCounter, mapData[widthCounter][heightCounter].myIconNumber);
  147.     }
  148.   }
  149.   infile.close();
  150. }
  151.  
  152.  
  153. void animap::reset()
  154. {
  155.   for (int widthCounter = 0; widthCounter < width; ++widthCounter)
  156.     for (int heightCounter = 0; heightCounter < height; ++heightCounter)
  157.       setSquare(widthCounter, heightCounter, mapData[widthCounter][heightCounter].myIconNumber);
  158. }
  159.  
  160. void animap::setSquare(word x, word y, byte icon_number)
  161. {
  162.   mapData[x][y].myIconNumber = icon_number;
  163.   mapData[x][y].myTerrainType = icon_number;
  164.   mapData[x][y].thisFrame = iconTable[icon_number].firstFrame;
  165. }
  166.  
  167. void animap::save(char * filename)
  168. {
  169.   ofstream outfile(filename, ios::in | ios::binary);
  170.   if (!outfile) return;
  171.   outfile << numIcons << '\n' << (int)squareWidth << '\n' << width << '\n' << height << '\n';
  172.   for (int counter = 0; counter < numIcons; ++counter)
  173.     outfile << iconNames[counter] << '\n';
  174.   for (word heightCounter = 0; heightCounter < height; ++heightCounter)
  175.   {
  176.     for (word widthCounter = 0; widthCounter < width; ++widthCounter)
  177.     {
  178.       outfile << mapData[widthCounter][heightCounter].myIconNumber;
  179.     }
  180.   }
  181.   outfile.close();
  182. }
  183.  
  184. void animap::loadIcon(int position, char * filename, yakLib * myYakLib)
  185. {
  186.   iconTable[position].addAll(filename, icon::fast, myYakLib);
  187.   strcpy(iconNames[position], filename);
  188. }
  189.  
  190. void animap::load(char * filename)
  191. {
  192.   ifstream infile(filename, ios::in | ios::binary);
  193.   if (!infile) return;
  194.   infile >> numIcons >> squareWidth >> width >> height;
  195.   for (int counter = 0; counter < numIcons; ++counter)
  196.   {
  197.     infile >> iconNames[counter];
  198.     loadIcon(counter, iconNames[counter]);
  199.   }
  200.   for (word heightCounter = 0; heightCounter < height; ++heightCounter)
  201.   {
  202.     for (word widthCounter = 0; widthCounter < width; ++widthCounter)
  203.     {
  204.       infile >> mapData[widthCounter][heightCounter].myIconNumber;
  205.       setSquare(widthCounter, heightCounter, mapData[widthCounter][heightCounter].myIconNumber);
  206.     }
  207.   }
  208.   infile.close();
  209. }
  210.  
  211.  
  212. void animap::draw(int centerx, int centery, int screenx, int screeny, int radx, int rady, word offset)
  213. {
  214.   int heightCounter, widthCounter, x, y;
  215.   for (heightCounter = -rady; heightCounter <= rady; ++heightCounter)
  216.   {
  217.     for (widthCounter = -radx; widthCounter <= radx; ++widthCounter)
  218.     {
  219.     x = (centerx + widthCounter) % (int)(width);
  220.     y = (centery + heightCounter) % (int)(height);
  221.     x += (x < 0) ? width : 0;
  222.     y += (y < 0) ? height : 0;
  223.     mapData[x][y].display(screenx + squareWidth*widthCounter - mapData[x][y].thisFrame->picture.width,
  224.     screeny + squareWidth*heightCounter - mapData[x][y].thisFrame->picture.height, offset, squareWidth);
  225.     }
  226.   }
  227.   lastMapX = centerx-radx;
  228.   lastMapY = centery-rady;
  229.   lastScreenX = screenx - squareWidth * radx;
  230.   lastScreenY = screeny - squareWidth * rady;
  231.   lastDrawWidth = 2*radx+1;
  232.   lastDrawHeight = 2*rady+1;
  233. }
  234.  
  235. void animap::drawXY(int left, int top, int screenLeft, int screenTop, int drawWidth, int drawHeight, word offset)
  236. {
  237.   int heightCounter, widthCounter, x, y;
  238.   for (heightCounter = 0; heightCounter < drawHeight; ++heightCounter)
  239.   {
  240.     for (widthCounter = 0; widthCounter < drawWidth; ++widthCounter)
  241.     {
  242.     x = rollover(left + widthCounter, width);
  243.     y = rollover(top + heightCounter, height);
  244.     mapData[x][y].display(screenLeft + squareWidth*widthCounter - mapData[x][y].thisFrame->picture.width,
  245.     screenTop + squareWidth*heightCounter - mapData[x][y].thisFrame->picture.height, offset, squareWidth);
  246.     }
  247.   }
  248.   lastMapX = left;
  249.   lastMapY = top;
  250.   lastScreenX = screenLeft;
  251.   lastScreenY = screenTop;
  252.   lastDrawWidth = drawWidth;
  253.   lastDrawHeight = drawHeight;
  254. }
  255.  
  256. int max(int val1, int val2) {return((val1 > val2) ? val1 : val2);};
  257.  
  258. void animap::advance()
  259. {
  260.   int heightCounter, widthCounter, x, y;
  261.   for (heightCounter = 0; heightCounter < height; ++heightCounter)
  262.     for (widthCounter = 0; widthCounter < width; ++widthCounter)
  263.       mapData[widthCounter][heightCounter].advance();
  264. }
  265.  
  266.  
  267. void animap::show(int centerx, int centery, int screenx, int screeny, int radx, int rady, word offset)
  268. {
  269.   draw(centerx, centery, screenx, screeny, radx, rady, offset);
  270.   advance();
  271. }
  272.  
  273. void animap::showXY(int left, int top, int screenLeft, int screenTop, int width, int height, word offset)
  274. {
  275.   drawXY(left, top, screenLeft, screenTop, width, height, offset);
  276.   advance();
  277. }
  278.  
  279. void animap::smartRefresh(int left, int top, word offset)
  280. {
  281.   int heightCounter, widthCounter, x, y, oldX, oldY;
  282.   int xCounter, yCounter;
  283.   for (heightCounter = 0; heightCounter < lastDrawHeight; ++heightCounter)
  284.     for (widthCounter = 0; widthCounter < lastDrawWidth; ++widthCounter)
  285.     {
  286.       x = rollover(left + widthCounter, width);
  287.       y = rollover(top + heightCounter, height);
  288.       oldX = rollover(lastMapX + widthCounter, width);
  289.       oldY = rollover(lastMapY + heightCounter, height);
  290.       mapData[x][y].refresh = (mapData[x][y].refresh == 3) ? 2 : ((mapData[x][y].refresh == 2) ? 1 : 0);
  291.       if (mapData[x][y].thisFrame != mapData[oldX][oldY].thisFrame->nextFrame)
  292.         mapData[x][y].refresh = 1;
  293.       if (mapData[x][y].nextActor || mapData[oldX][oldY].nextActor)
  294.       {
  295.         mapData[x][y].refresh=1;
  296.         mapData[rollover(x-1, width)][y].refresh =1;
  297.         mapData[x][rollover(y-1, height)].refresh =1;
  298.         mapData[rollover(x-1,width)][rollover(y-1,height)].refresh =1;
  299.       }
  300.     }
  301.   for (heightCounter = 0; heightCounter < lastDrawHeight; ++heightCounter)
  302.   {
  303.     for (widthCounter = 0; widthCounter < lastDrawWidth; ++widthCounter)
  304.     {
  305.       x = rollover(left + widthCounter, width);
  306.       y = rollover(top + heightCounter, height);
  307.       oldX = rollover(lastMapX + widthCounter, width);
  308.       oldY = rollover(lastMapY + heightCounter, height);
  309.     if (mapData[x][y].refresh)
  310.       mapData[x][y].display(lastScreenX + squareWidth*widthCounter - mapData[x][y].thisFrame->picture.width,
  311.     lastScreenY + squareWidth*heightCounter - mapData[x][y].thisFrame->picture.height, offset, squareWidth);
  312.     }
  313.   }
  314.   lastMapX = left;
  315.   lastMapY = top;
  316. }
  317.  
  318. void animap::randomize()
  319. {
  320.   for (int heightCounter = 0; heightCounter < height; ++heightCounter)
  321.     for (int widthCounter = 0; widthCounter < width; ++widthCounter)
  322.       setSquare(widthCounter, heightCounter, random(numIcons));
  323. }